package accountManagement.managedbean;

import accountManagement.entity.Employee;
import accountManagement.sessionbean.EmployeeManager;

import accountManagement.sessionbean.PasswordHashSessionBean;

import java.io.IOException;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;

@ManagedBean
@ViewScoped
public class ResetPasswordManagedBean {

    @EJB
    private EmployeeManager employeeManagerSessionBeam;
    @EJB
    private PasswordHashSessionBean passowordHashSessionBean;
    private String userName;
    private String DOB;
    private Employee employee;

    public ResetPasswordManagedBean() {
    }

    public void doVerify(ActionEvent event) throws IOException {
        employee = employeeManagerSessionBeam.getEmployeeByUserName(userName);
        if (employee == null) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Invalid UserName", ""));

        } else {

            if (employee.getDOB().equals(DOB)) {
                FacesContext.getCurrentInstance().getExternalContext().getFlash().put("employee", employee);
                FacesContext.getCurrentInstance().getExternalContext().redirect("resetPassword2.xhtml");

            } else {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Verification Failed.", ""));

            }
        }
    }

    public EmployeeManager getEmployeeManagerSessionBeam() {
        return employeeManagerSessionBeam;
    }

    public void setEmployeeManagerSessionBeam(EmployeeManager employeeManagerSessionBeam) {
        this.employeeManagerSessionBeam = employeeManagerSessionBeam;
    }

    public void retrieveEmployee() {
        employee = employeeManagerSessionBeam.getEmployeeByUserName(userName);
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getDOB() {
        return DOB;
    }

    public void setDOB(String DOB) {
        this.DOB = DOB;
    }

    public Employee getEmployee() {
        return employee;
    }

    public void setEmployee(Employee employee) {
        this.employee = employee;
    }
}
